<%--
  Created by IntelliJ IDEA.
  User: 10475
  Date: 2023/2/18
  Time: 14:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>招聘用户列表</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/lib/layui-v2.6.3/css/layui.css" media="all">
</head>
<body>

<table id="demo" lay-filter="test"></table>
<!-- 引入jquery，需要联网才能使用 -->
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>

<script src="${pageContext.request.contextPath}/layui/layui.js"></script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">状态编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
    layui.use('table', function(){
        var table = layui.table;
        //第一个实例
        table.render({
            elem: '#demo'
            ,height: 'full-20'
            ,url: '${pageContext.request.contextPath}/SelectAllCompanyServlet' //数据接口
            ,method:'get'
            ,page: true //开启分页
            ,cols: [[ //表头
                {type:'checkbox'}
                ,{field: 'companyId', title: '账号', width:150, sort: true}
                ,{field: 'companyName', title: '公司名', width:150, edit: 'text'}
                ,{field: 'companyAddress', title: '公司地址', width:150, sort: true, edit: 'text'}
                ,{field: 'companyTel', title: '公司电话', width:150, edit: 'text'}
                ,{field: 'companyEmail', title: '公司邮箱', width: 150, edit: 'text'}
                ,{field: 'addTime', title: '注册时间', width: 150, sort: true, edit: 'text'}
                ,{field: 'gongNum', title: '工商号', width: 150}
                ,{field: 'state', title: '状态', width: 150, sort: true, edit: 'text'}
                ,{field: 'right', title: '操作', width: 150, toolbar:'#barDemo'} //toolbar 是工具栏
            ]]
        });

        //监听行事件
        //触发单元格工具事件
        table.on('tool(test)', function(obj){ // 双击 toolDouble
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    obj.del();
                    layer.close(index);
                });
            }
            else if(obj.event === 'edit'){
                //实现编辑功能
                //打开弹出层
                layer.open({
                    type:2
                    ,title:'编辑企业信息'
                    ,content:'${pageContext.request.contextPath}/jsp/Admin/edit_company.jsp'
                    //是否最大化
                    ,maxmin:true
                    ,area:['500px','450px']
                    ,btn:['确定','取消']
                    //回调函数
                    //参数 索引和layero
                    ,yes:function (index,layero) {
                        //回调的数值
                        var companyId=$(layero).find('iframe')[0].contentWindow.companyid.value;
                        var companyName=$(layero).find('iframe')[0].contentWindow.document.getElementById('companyname').value;
                        var companyaddress=$(layero).find('iframe')[0].contentWindow.document.getElementById('companyaddress').value;
                        var companytel=$(layero).find('iframe')[0].contentWindow.document.getElementById('companytel').value;
                        var companyemail=$(layero).find('iframe')[0].contentWindow.document.getElementById('companyemail').value;
                        var addtime=$(layero).find('iframe')[0].contentWindow.document.getElementById('addtime').value;
                        var gongshangnum=$(layero).find('iframe')[0].contentWindow.document.getElementById('gongshangnum').value;
                        var state=$(layero).find('iframe')[0].contentWindow.document.getElementById('state').value;

                        //同步数据表格中的数据
                        obj.update({
                            'companyid':companyid,
                            'companyname':companyname,
                            'companyaddress':companyaddress,
                            'companytel':companytel,
                            'companyemail':companyemail,
                            'addtime': addtime,
                            'gongshangnum':gongshangnum,
                            'state':state,
                        })

                        //	<!--在线引入jquery后正常使用 -->
                        $.ajax({
                            url:'EditCompany?companyid='+companyid+"&companyname="+companyname+"&companyaddress="+companyaddress+"&companytel="+companytel+"&companyemail="+companyemail+"&addtime="+addtime+"&gongshangnum="+gongshangnum+"&state="+state,
                            type:'post',
                            contentType:'application/json;charset=utf-8',
                            dataType:'text',
                            data:{
                                'companyid':companyid,
                                'companyname':companyname,
                                'companyaddress':companyaddress,
                                'companytel':companytel,
                                'companyemail':companyemail,
                                'addtime': addtime,
                                'gongshangnum':gongshangnum,
                                'state':state,
                            },
                            success:function(res){
                                if("success"==res){
                                    alert("恭喜你，编辑成功！")
                                }else {
                                    layer.msg("实在抱歉，编辑失败！");
                                }

                            },
                            error:function(res){
                                layer.msg("实在抱歉，编辑失败！");
                            }
                        })
                        layer.close(index);//关闭弹窗
                    },success:function (layero,index) {
                        var div = layero.find('iframe').contents().find('#useradmin');
                        var body = layer.getChildFrame('body',index);
                        var iframeWindow=window['layui-layer-iframe'+index];
                        //获取未修改前的默认数值到编辑框中，方便用户修改自己想修改的部分
                        body.find('#companyid').val(data.companyid);
                        body.find('#companyname').val(data.companyname);
                        body.find('#companyaddress').val(data.companyaddress);
                        body.find('#companytel').val(data.companytel);
                        body.find('#companyemail').val(data.companyemail);
                        body.find('#addtime').val(data.addtime);
                        body.find('#gongshangnum').val(data.gongshangnum);
                        body.find('#state').val(data.state);

                    }
                })

            }

        });


        // 单元格编辑后的事件
        table.on('edit(test)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            layer.msg('[ID: '+data.companyId+'] ' + field + ' 字段更改值为：'+ value);

            $.ajax({
                url:'EditCompany?companyId='+data+"&companyname="+companyname+"&companyaddress="+companyaddress+"&companytel="+companytel+"&companyemail="+companyemail+"&addtime="+addtime+"&gongshangnum="+gongshangnum+"&state="+state,
                type:'post',
                contentType:'application/json;charset=utf-8',
                dataType:'text',
                data:{
                    'companyid':companyid,
                    'companyname':companyname,
                    'companyaddress':companyaddress,
                    'companytel':companytel,
                    'companyemail':companyemail,
                    'addtime': addtime,
                    'gongshangnum':gongshangnum,
                    'state':state,
                },
                success:function(res){
                    if("success"==res){
                        alert("恭喜你，编辑成功！")
                    }else {
                        layer.msg("实在抱歉，编辑失败！");
                    }

                },
                error:function(res){
                    layer.msg("实在抱歉，编辑失败！");
                }
            })
            layer.close(index);//关闭弹窗

        });
    });
</script>
</body>
</html>